package com.github.baichuan.application.scaffold.rest.dto.realm;

import com.github.baichuan.application.scaffold.rest.domain.application.Realm;
import com.github.baichuan.application.scaffold.rest.jpa.application.ApplicationRepository;
import com.github.baichuan.mapping.BeanMapper;
import com.github.baichuan.mapping.Mapping;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Schema(title = "应用程序域")
@Getter
public class RealmTableView {
    @Mapping
    private String id;

    @Schema(title = "域名称")
    @Mapping
    private String name;

    @Schema(title = "应用数量")
    private int applications;

    @Component
    public static class Mapper {
        @Autowired
        private ApplicationRepository applicationRepository;

        public RealmTableView map(Realm realm){
            RealmTableView view = BeanMapper.map(realm, RealmTableView.class);
            view.applications = applicationRepository.countByRealm(realm);
            return view;
        }
    }
}
